package com.example.demo.controller;

import com.example.demo.controller.data.ReturnData;
import com.example.demo.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 上传文件相关的接口
 */
@RestController
public class UploadController {
    @Autowired
    UploadService uploadService;
    /**
     * 上传头像的业务方法
     * @param file 一个头像文件
     * @return
     */
    @PostMapping("/uploadAvatar")
    public ReturnData uploadAvatar(MultipartFile file, HttpServletRequest request) throws IOException {
        String token = request.getHeader("token");//取Token
        return uploadService.uploadAvatar(file,token);
    }

    /**
     * 通用的上传文件方法
     * @param file  文件
     * @param type  业务类型   1 修改头像   2  是朋友圈的图片   3 轮播图     。。。。
     * @param request  请求头
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadFile")
    public ReturnData uploadFile(MultipartFile file,int type, HttpServletRequest request) throws IOException {
        String token = request.getHeader("token");//取Token
        return uploadService.uploadFile(file,type,token);
    }
}
